﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Provider;
using System.Configuration;
using DbDoc.BusinessObjects;
using System.Reflection;
using System.IO;

namespace DbDoc.Provider.Source
{
    public abstract class SourceProvider
    {
        public abstract string ConnectionInfo { get; }
        public abstract string StorageName { get; }
        public abstract IEnumerable<string> ExcludedDatabasesList { get; }

        //Methods
        public abstract IList<string> GetServerDatabases();
        public abstract IList<string> GetServerTables(string database);
        public abstract IList<string> GetServerStoredProcs(string database);
        public abstract IList<string> GetServerStoredProcs(string database, bool includeSystemObjects);
        public abstract IList<string> GetServerFunctions(string database);
        public abstract IList<string> GetServerViews(string database);
        public abstract IList<string> GetServerColumns(string database, string table);

        public abstract void Initialize();

        public string ExcludedDatabases
        {
            get
            {
                return string.Join(", ", ExcludedDatabasesList);
            }
        }
    }

    public class SourceProviderFactory
    {
        static SourceProviderFactory()
        {
        }

        public static SourceProvider Create(string providerType)
        {
            SourceProvider provider = null;
            bool breakLoop = false; ;
            DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            FileInfo[] dlls = dir.GetFiles("*.dll");
            Type sourceProviderType = Assembly.LoadFrom("Provider.Source.dll").GetTypes()[0];
            foreach (FileInfo dll in dlls)
            {
                if (breakLoop) break;

                Assembly a = Assembly.LoadFrom(dll.Name);
                Type[] types = a.GetTypes();

                foreach (Type t in types)
                    if (t.BaseType == sourceProviderType && string.Compare(t.Name, providerType) == 0)
                    {
                        provider = (SourceProvider)Activator.CreateInstance(t);
                        breakLoop = true;
                        break;
                    }
            }

            // Initialize the instance of the provider
            if(provider != null)
                provider.Initialize();

            return provider;
        }
    }

    public class SourceProviderManager
    {
        //Initialization related variables and logic
        private static bool isInitialized = false;
        private static Exception initializationException;

        private static object initializationLock = new object();

        static SourceProviderManager()
        {
            Initialize();
        }

        private static void Initialize()
        {

            try
            {
                defaultProvider = SourceProviderFactory.Create("SqlSourceProvider");
                if (defaultProvider == null)
                {
                    throw new ApplicationException("Source Provider, for the database which needs to be documented is not configured.");
                }
            }
            catch (Exception ex)
            {
                initializationException = ex;
                isInitialized = false;
                throw ex;
            }

            isInitialized = true; //error-free initialization
        }

        //Public feature API
        private static SourceProvider defaultProvider;

        public static SourceProvider Provider
        {
            get
            {
                return defaultProvider;
            }
        }

    }
}
